﻿using System;

namespace DataScripter.Database.SqlServer
{
    //TODO: unit tests schrijven
    class SqlServerStatementCreator : ISqlServerStatementCreator
    {
        private static string QuotedTable(string schemaName, string tableName)
        {
            return "[" + schemaName + "].[" + tableName + "]";
        }

        public string CreateForeignKey(IForeignKey foreignKey)
        {
            return "ALTER TABLE " + QuotedTable(foreignKey.SchemaName, foreignKey.TableName) +
                   "  WITH CHECK ADD  CONSTRAINT [" + foreignKey.Name + "] FOREIGN KEY([" + foreignKey.ReferencedColumnName +
                   "]) REFERENCES " + QuotedTable(foreignKey.ReferencedSchemaName, foreignKey.ReferencedTableName) + " ([" + foreignKey.ReferencedColumnName + "])";
        }

        public string DropForeignKey(IForeignKey foreignKey)
        {
            return "ALTER TABLE " + QuotedTable(foreignKey.SchemaName, foreignKey.TableName) +
                    " DROP CONSTRAINT [" + foreignKey.Name + "]";
        }

        public string TruncateTable(ITableStructure table)
        {
            return "TRUNCATE TABLE " + QuotedTable(table.SchemaName, table.TableName);
        }
    }
}
